<?php


namespace App\Admin\Actions\BatchAction;


use App\Admin\Actions\BatchAction;
use App\Models\AdminUser;
use App\Patent;
use App\PatentApply;
use Illuminate\Database\Eloquent\Collection;

class CancelPatentGrantBatch extends BatchAction
{
    public $name = '退回到申请中';

    public function handle(Collection $collection)
    {
        foreach ($collection as $item) {
            if ($item instanceof PatentApply) {
                Patent::query()->where('patent_sn', $item->patent_sn)->delete();
                $item->status = 0;
                $item->patent_sn = null;
                $item->apply_date = null;
                $item->publication_num = null;
                $item->publication_date = null;
                $item->save();
            }
        }
        return $this->response()->success('操作成功')->refresh();
    }

    public function dialog()
    {
        return $this->confirm("授权信息将被删除\n确定要退回到申请中吗？");
    }


    public function authorize(AdminUser $user, Collection $collection)
    {
        return $user->role_type == 1;
    }


}
